﻿using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace bmapi.Controllers
{
    [Route("taskmove")]
    public class TaskController : BuyiController
    {
        /// <summary>
        /// 工作移交(工序移交)
        /// </summary>
        /// <param name="obj"></param>
        [HttpPost]
        public HttpResponseMessage MoveTask(dynamic obj)
        {
            if (obj == null)
                return AssistFun.toJson(new { status=201,tip="请提供相应参数"} );
            User user = GetUserByLoginID(GetCrrentIDByToken());
            if(user==null || user.power!=1)
                return AssistFun.toJson(new { status = 201, tip = "无权处理工作移交" });
            
            int pcid = Convert.ToInt32(obj.Id);  //流程ID
            int code= Convert.ToInt32(obj.code); //移交工序 如果是37那么只有主订单有日志记录
            int uid= Convert.ToInt32(obj.LoginiId); //移交给谁/或者说分配给谁
            SQLCommand sc = null;  
            //找出流程
            string sql = "select pc_id,PLID,bind_id,step,station,download from PC_tb where pc_id=@pcid and step=@code";
            sc = new SQLCommand(sql);
            sc.Add("@pcid", pcid);
            sc.Add("@code", code);
            DataRow dr = sc.ExecuteOneRow();
            if(dr==null)
                return AssistFun.toJson(new { status = 201, tip = "没有找到当前流程中的这道工序" });
            //修改工序执行者  并且修改下载状态(也可以不修改状态  让其重新下载  可是以防该用户不执行冲下载)  (绑定时  bind_id=bind_id ) 不绑pc_id=@pcid
            int bindid = Convert.ToInt32(dr["bind_id"]);
            if (bindid == 0)
                sql = "update PC_tb set station=@loginid,download=0 where pc_id=@pcid";
            else
            {
                sql = "update PC_tb set station=@loginid,download=0 where bind_id=@pcid";
                pcid = bindid;                
            }
            sc = new SQLCommand(sql);
            sc.Add("@loginid", uid);
            sc.Add("@pcid", pcid);            
            List<SQLCommand> cmds = new List<SQLCommand>();
            cmds.Add(sc);
            //修改下载状态那么这条日志记录也删除  (绑定时  id =bindid  ) id=pcid  
            sql = "select user_id from Task_tb where id=@pcid and code=@code";
            sc = new SQLCommand(sql);
            sc.Add("@pcid", pcid);
            sc.Add("@code", code);
            string resid = sc.ExecuteScalar();
            int oldUid = resid == null ? 0: Convert.ToInt32(resid);
            //(37 && 绑定 id=bind_id 绑定 && !=37 id in(orderids)) 不绑 id=@pcid
            if (bindid != 0 && code != 37)
                sql = "delete from Task_tb where code=@code and user_id=@loginid and id in (select pc_id from PC_tb where bind_id =@pcid)";
            else
                sql = "delete from Task_tb where id=@pcid and code=@code and user_id=@loginid";
            sc = new SQLCommand(sql);
            sc.Add("@pcid", pcid);
            sc.Add("@code", code);
            sc.Add("@loginid", oldUid);
            cmds.Add(sc);
            int res = SQLCommand.ExecTran(cmds);
            if(res==0)
                return AssistFun.toJson(new { status = 201, tip = "任务移交失败" });
            else
                return AssistFun.toJson(new { status = 200, tip = "成功" });
        }

        /// <summary>
        /// 特体移交
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPut]
        public HttpResponseMessage MovePod(dynamic obj)
        {
            try
            {
                if (obj == null)
                    return AssistFun.toJson(new { status = 201, tip = "请提供相应参数" });
                User user = GetUserByLoginID(GetCrrentIDByToken());
                if (user == null || user.power != 1)
                    return AssistFun.toJson(new { status = 201, tip = "无权处理工作移交" });

                int pcid = Convert.ToInt32(obj.Id);  //流程ID
                int pod = Convert.ToInt32(obj.Pod); //移交工序
                int uid = Convert.ToInt32(obj.LoginiId); //移交给谁/或者说分配给谁

                string sql = "update Pod_tb set user_id=@loginid,time=@time where order_id=@orderid and pod=@pod ";
                SQLCommand sc = new SQLCommand(sql);
                sc.Add("@loginid", uid);
                sc.Add("@time", DateTime.Now, true);
                sc.Add("@orderid", pcid);
                sc.Add("@pod", pod);
                int res = sc.ExecuteNonQuery();
                if (res == 0)
                    return AssistFun.toJson(new { status = 201, tip = "工作移交失败" });
                return AssistFun.toJson(new { status = 200, tip = "成功" });
            }
            catch (Exception ex)
            {
                return AssistFun.toJson(new { status = 201, tip = "异常:"+ex.Message });
            }
            
        }
        /// <summary>
        /// 获取可以接手移交工序的员工   
        /// </summary> 
        /// <param name="obj"></param>
        [HttpGet]
        public HttpResponseMessage GetUserCanAccept(int code,int LoginiId,int pcid=0,int pod=0)
        {

            User user = GetUserByLoginID(GetCrrentIDByToken());
            string sql = "select login_id as LoginId,name as Name from User_tb where PLID=@plid and (code=@code or code2=@code or code3=@code) and login_id != @loginid and power=0 and state=0";
            int podUid = 0;
            if (pcid != 0)
            {
                sql = "select user_id from Pod_tb where order_id=@orderid and pod=@pod";
                SQLCommand poduser = new SQLCommand(sql);
                poduser.Add("@orderid", pcid);
                poduser.Add("@pod", pod);
                podUid = Convert.ToInt32(poduser.ExecuteScalar());
                sql = "select login_id as LoginId,name as Name from User_tb where PLID=@plid and (code=@code or code2=@code or code3=@code) and login_id !=@podUid and power=0 and state=0";
            }
            SQLCommand sc = new SQLCommand(sql);
            if (pcid != 0)
                sc.Add("@podUid",podUid );
            else
                sc.Add("@loginid", LoginiId);

            sc.Add("@plid", user.PLID);
            sc.Add("@code", code);            
            DataTable dt = sc.ExecuteScalars();
            if (dt == null)
                return AssistFun.toJson(new { status = 201, tip = "没有找到匹配员工" });
            else
            {
                List<Accpter> accpters = AssistFun.DataTableToList<Accpter>(dt);
                return AssistFun.toJson(new { status = 200, tip = "成功",data=new {total=accpters.Count,rows=accpters } });
            }
        }

    }
    public class Accpter
    {
        public int LoginId { get; set; }
        public string Name { get; set; }
    }
}
